home *** CD-ROM | disk | FTP | other *** search
/ Univers Interactif 3 / INTERACTIF.BIN / pc / planeten / internet / machttp2.sit / MacHTTP 2.0 / MacHTTP Software & Docs / Tutorials / Extending MacHTTP Scripts / FormScript1.txt < prev    next >
Text File  |  1994-12-11  |  6KB  |  137 lines

  1. -- set these properties outside of the event.  That way they will only be set 
  2. -- once each time the app is run, not once for each event.
  3. property crlf : (ASCII character 13) & (ASCII character 10)
  4. -- This is a standard header for HTML files.
  5. property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & ┬
  6.     "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
  7. -- Idletime is how long you want it to remain open to 
  8. -- wait for another event. Idletime is in seconds.
  9. -- Datestamp will contain the current date.  Initialize it here.
  10. property idletime : 300 -- set to 5 minutes
  11. property datestamp : 0
  12.  
  13. -- this bit of code outside the sdoc event is executed at launch-time
  14. -- it is neccesary because an idle event can happen between
  15. -- launch and the receipt of an sdoc event (and in fact often does)
  16. set datestamp to current date
  17.  
  18. -- This is the loop for handling "sdoc" AppleEvents sent to the 
  19. -- application by MacHTTP.
  20. on ╟event WWW╜sdoc╚ path_args ┬
  21.     given ╟class kfor╚:http_search_args, ╟class post╚:post_args, ╟class meth╚:method, ╟class addr╚:client_address, ╟class user╚:username, ╟class pass╚:password, ╟class frmu╚:from_user, ╟class svnm╚:server_name, ╟class svpt╚:server_port, ╟class scnm╚:script_name, ╟class ctyp╚:content_type
  22.     -- Variables available for use:
  23.     -- http_search_args - stuff in the URL after a ?
  24.     -- post_args - stuff in the URL after a $
  25.     -- method - GET, POST, etc. Used to tell if post_args are valid
  26.     -- client_address - IP address or domain name of remote client's host
  27.     -- from_user - non-standard. e-mail address of remote user
  28.     -- username - authenticated user name
  29.     -- password - authenticated password
  30.     -- server_name - name or IP address of this server
  31.     -- server_port - TCP/IP port number being used by this server
  32.     -- script_name - URL name of this script
  33.     -- content_type - MIME content type of post_args
  34.     
  35.     -- Using the "try" clause causes the "on error" routine to be run
  36.     -- if an error occurs instead of crashing.
  37.     -- If the error occurs outside the "try"/"end try" space then
  38.     -- the "on error" routine is NOT run.
  39.     try
  40.         -- save the current date and time to check later for quitting
  41.         set datestamp to current date
  42.         
  43.         -- Return each parameter so you can see what the 
  44.         -- decoded information looks like.
  45.         -- I use DecodeURL and Tokenize only on the post_args, since this is the only 
  46.         -- information entered by the user (all else is from MacHTTP).
  47.         set return_page to http_10_header ┬
  48.             & ┬
  49.             "<HTML><HEAD><TITLE>Post_Args Results</TITLE></HEAD>" & "<BODY><H1>Post_Args Results</H1>" & return ┬
  50.             & "<H4>post_args</H4>" & return
  51.         
  52.         -- this will produce a list of "name=value" pairs 
  53.         set postarglist to tokenize post_args with delimiters {"&"}
  54.         
  55.         -- process each list pair in postarglist
  56.         -- store the original AppleScript text item delimiters
  57.         set oldDelim to AppleScript's text item delimiters
  58.         -- use "=" to delimit the pairs
  59.         set AppleScript's text item delimiters to {"="}
  60.         -- traverse the list and process the items
  61.         repeat with currpostarg in postarglist
  62.             set currname to first text item of currpostarg
  63.             if currname = "name" then
  64.                 set username to (Decode URL (dePlus (last text item of currpostarg)))
  65.                 set return_page to return_page & "<B>User: </B>" & username & return
  66.             else if currname = "address" then
  67.                 set useraddress to (Decode URL (dePlus (last text item of currpostarg)))
  68.                 set return_page to return_page & "<BR><B>E-mail: </B>" & useraddress & return
  69.             else if currname = "sub" then
  70.                 set sub_text to (Decode URL (dePlus (last text item of currpostarg)))
  71.                 set return_page to return_page & "<BR><B>Subject:</B><BR>" & sub_text & return
  72.             else if currname = "message" then
  73.                 set message_text to (Decode URL (dePlus (last text item of currpostarg)))
  74.                 set return_page to return_page & "<BR><B>Message:</B><BR>" & message_text & return
  75.             else if currname = "S" then
  76.                 -- ignore it.  That's the Submit button.
  77.             else
  78.                 -- you have a variable who's name you don't know.  Bad news!
  79.                 -- create your own errMsg and errNum to pass back
  80.                 -- the number 100 has no significance.  I just chose it at random
  81.                 error ("Unknown variable in post_args: " & currname) number 100
  82.             end if
  83.         end repeat
  84.         -- restore the old AppleScript text item delimiters settings
  85.         set AppleScript's text item delimiters to oldDelim
  86.         
  87.         set return_page to return_page ┬
  88.             & "<H4>client_address</H4>" & return ┬
  89.             & client_address & return ┬
  90.             & "<HR><I>Results generated at: " & (current date) ┬
  91.             & "</I>" & "</BODY></HTML>"
  92.         -- return the page created.  A return statement ends the 
  93.         -- processing of the AppleEvent
  94.         return return_page
  95.         
  96.         -- here is the routine to run if an error occurs
  97.         -- errMsg contains the message sent by the System
  98.         -- errNum contains the number of the error (negative for System, AE, or AS errors)
  99.     on error errMsg number errNum
  100.         -- create a page of HTML text to return
  101.         set return_page to http_10_header ┬
  102.             & ┬
  103.             "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" & "<BODY><H1>Error Encountered!</H1>" & return ┬
  104.             & "An error was encountered while trying to run this script." & return
  105.         set return_page to return_page ┬
  106.             & "<H3>Error Message</H3>" & return & errMsg & return ┬
  107.             & "<H3>Error Number</H3>" & return & errNum & return ┬
  108.             & "<H3>Date</H3>" & return & (current date) & return
  109.         set return_page to return_page ┬
  110.             & ┬
  111.             "<HR>Please notify Jon Wiederspan at " & ┬
  112.             "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" & " of this error." & "</BODY></HTML>"
  113.         -- return the page created.  A return statement ends the 
  114.         -- processing of the AppleEvent
  115.         return return_page
  116.     end try
  117. end ╟event WWW╜sdoc╚
  118.  
  119. -- The idle function is run everytime the system sends an idle message.
  120. -- If the current date is more than idletime seconds more than the last date 
  121. -- then it is time to quit.
  122. -- The return value tells the system how long to wait before
  123. on idle
  124.     if (current date) > (datestamp + idletime) then
  125.         quit
  126.     end if
  127.     return 5
  128. end idle
  129.  
  130. -- This code allows you to do any clean-up that might be necessary.
  131. -- Example: you could write the quit event time to a log to see 
  132. -- how often the applet is running.
  133. on quit
  134.     -- do any clean-up chores here
  135.     continue quit
  136. end quit
  137.